package com.neusoft.shopping.manager.dao;

import com.neusoft.shopping.manager.entity.PictureEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.InvalidResultSetAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

@Repository
public class PictureDAOImpl {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    RowMapper<PictureEntity> rowMapper=new BeanPropertyRowMapper<>(PictureEntity.class);

    public List<PictureEntity> qureyAll(){
        String  sql="select *from pictureinfo";
        return jdbcTemplate.query(sql,rowMapper);
    }

    public PictureEntity qureyByGoodsno(Integer goodsno){
        String  sql="select *from pictureinfo where goodsno=?";
        try {
            PictureEntity pictureEntity= jdbcTemplate.queryForObject(sql,rowMapper,goodsno);
            return pictureEntity;
        }catch (InvalidResultSetAccessException e){
            return null;
        }catch (DataAccessException e){
            return null;
        }
    }

    public PictureEntity qureyFirst(Integer goodsno){
        String  sql="select *from pictureinfo where goodsno=? and isfirst=Y";
        return jdbcTemplate.queryForObject(sql,rowMapper,goodsno);
    }

    public int add_picture(PictureEntity picture){
        String sql="insert into pictureinfo values (null,?,?,?)";
        return jdbcTemplate.update(sql,picture.getGoodsno(),picture.getUrl(),picture.getIsfirst());
    }

    public int update_stockByPictureno(PictureEntity picture){
        String sql="update pictureinfo set goodsno=?,url=?,isfirst=? where pictureno=?";
        return jdbcTemplate.update(sql,picture.getGoodsno(),picture.getUrl(),picture.getIsfirst(),picture.getPictureno());
    }
    public int del_picture(Integer pictureno){
        String sql="delete from pictureinfo where picturekno=?";
        return jdbcTemplate.update(sql,pictureno);
    }
}
